#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include  <errno.h>

void signal_handler(int sig)
{
    printf("handler: sig = %d\n", sig);
}

int r_read(char* data, int len)
{
    int ret = -1;

    while (data &&
           (ret = read(STDIN_FILENO, data, len - 1)) == -1 &&
           (errno == EINTR) )
    {
        printf("restart syscall manually...\n");
    }

    if (ret != -1) {
        data[len] = 0;
    }

    return ret;
}

int main(void)
{
    char buf[32] = {0};
    signal(SIGINT, signal_handler);
    // sysv_signal(SIGINT, signal_handler);
    // bsd_signal(SIGINT, signal_handler);

    r_read(buf, 32);
    printf("input: %s\n", buf);

    return 0;
}